#version 450 core

layout(binding = 0) uniform sampler2D tex;

in Varyings
{
    vec2 uv;
} i;

out vec4 fragColor;

// 卷积半径
const float r = 1.0 / 300.0;

void main()
{
    // 用3x3卷积实现

    // 采样点
    vec2 offsets[9] = {
    vec2(-r, r),
    vec2(0.0, r),
    vec2(r, r),
    vec2(-r, 0.0),
    vec2(0.0, 0.0),
    vec2(r, 0.0),
    vec2(-r, -r),
    vec2(0.0, -r),
    vec2(r, -r)
    };

    // 卷积
    float kernel[9] = {
        -1, -1, -1,
        -1, 9, -1,
        -1, -1, -1
    };

    vec4 color = vec4(0.0);
    color += kernel[0] * texture(tex, i.uv + offsets[0]);
    color += kernel[1] * texture(tex, i.uv + offsets[1]);
    color += kernel[2] * texture(tex, i.uv + offsets[2]);
    color += kernel[3] * texture(tex, i.uv + offsets[3]);
    color += kernel[4] * texture(tex, i.uv + offsets[4]);
    color += kernel[5] * texture(tex, i.uv + offsets[5]);
    color += kernel[6] * texture(tex, i.uv + offsets[6]);
    color += kernel[7] * texture(tex, i.uv + offsets[7]);
    color += kernel[8] * texture(tex, i.uv + offsets[8]);

    fragColor = color;
}
